
<?php
//模块插件类
//插件功能类，由系统调用，实现具体功能。
defined('BY_JYA') or exit('error');
class core_jy_kefu extends plugin{
	public function payresult($message){
		
	}
	public function receive($message){
		if($message['Event']=='subscribe'){
			//这里编写关注事件处理代码
			
		}
		if($message['Event']=='unsubscribe'){
			//这里编写取消关注事件处理代码
			
		}
	}
	public function wechat($message,$back) {
		$senddata['touser'] = $message['FromUserName'];
		$senddata['msgtype'] = 'text';
		if(!empty($message['FromUserName'])){
			$openid = $message['FromUserName'];
		}else{
			$text = '认证服务号才能使用微信客服哦！';
			$senddata['text']['content'] = $text;
			$res = cfc('account')->sendCustomNotice($senddata);
			return null;
		}
		$item=pdo_get('jy_kefus',array('keyid'=>$back['id']));
		$kf_account = $item['name'].'@'.$item['mpname'];
		$content = cfc('kefu')->setkfsession($kf_account,$openid);
		if($content['errcode'] != 0){
			$text = cfc('kefu')->error_code($content['errcode']);
			$senddata['text']['content'] = $text;
			$res = cfc('account')->sendCustomNotice($senddata);
			return null;
		}
		$text = $item['welcometext'] ? $item['welcometext'] : '您好！我是客服：'.$item['nickname'].'。有任何问题，请直接发信息咨询！';
		$senddata['text']['content'] = $text;
		$senddata['customservice']=$kf_account;
		$res = cfc('account')->sendCustomNotice($senddata);
		return null;
	}
	public function qr_scan($qr){
		$senddata['touser'] = $qr['openid'];
		$senddata['msgtype'] = 'text';
		$data=json_decode($qr['data'],true);
		if(!empty($qr['openid'])){
			$openid = $qr['openid'];
		}else{
			$text = '认证服务号才能使用微信客服哦！';
			$senddata['text']['content'] = $text;
			$res = cfc('account')->sendCustomNotice($senddata);
			return null;
		}
		$item=pdo_get('jy_kefus',array('keyid'=>$data['keyid']));
		$kf_account = $item['name'].'@'.$item['mpname'];
		$content = cfc('kefu')->setkfsession($kf_account,$openid);
		if($content['errcode'] != 0){
			$text = cfc('kefu')->error_code($content['errcode']);
			$senddata['text']['content'] = $text;
			$res = cfc('account')->sendCustomNotice($senddata);
			return null;
		}
		$text = $item['welcometext'] ? $item['welcometext'] : '您好！我是客服：'.$item['nickname'].'。有任何问题，请直接发信息咨询！';
		$senddata['text']['content'] = $text;
		$senddata['customservice']=$kf_account;
		$res = cfc('account')->sendCustomNotice($senddata);
		return null;
	}
}
?>
